package com.solo.common.core.utils;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;

import java.util.ArrayList;
import java.util.List;

public class TreeUtils extends TreeUtil {

    /**
     * 获取子节点id集合
     * @param node node
     * @param includeCurrentNode 是否包括当前节点
     * @return {@link List }<{@link T }>
     */
    public static <T> List<T> getChildrenIds(Tree<T> node, boolean includeCurrentNode) {
        final List<T> result = new ArrayList<>();
        if (null == node) {
            return result;
        }

        if (includeCurrentNode) {
            result.add(node.getId());
        }

        collectIds(node, result);
        
        return result;
    }


    /**
     * 递归获取树节点的子id集合
     * @param tree 树
     * @param ids ids
     */
    private static <T> void collectIds(Tree<T> tree, List<T> ids) {
        if (tree != null && tree.getChildren() != null) {
            tree.getChildren().forEach(child -> {
                ids.add(child.getId());
                collectIds(child, ids);
            });
        }
    }

}
